package leetcode.stackandqueue;

public class DesignCircularQueue622 {
    class MyCircularQueue1 {
        int[] data;
        int start;
        int end;
        int size;

        public MyCircularQueue1(int k) {
            data = new int[k];
            start = -1;
            end = -1;
            size = k;
        }

        public boolean enQueue(int value) {
            if (isFull()) {
                return false;
            }
            if (start == -1) {
                start = 0;
            }
            end = (end + 1) % size;
            data[end] = value;
            return true;
        }

        public boolean deQueue() {
            if (isEmpty()) {
                return false;
            }
            if (start == end) {
                start = -1;
                end = -1;
                return true;
            }
            start = (start + 1) % size;
            return true;
        }

        public int Front() {
            if (isEmpty()) {
                return -1;
            }
            return data[start];
        }

        public int Rear() {
            if (isEmpty()) {
                return -1;
            }
            return data[end];
        }

        public boolean isEmpty() {
            return start == -1;
        }

        public boolean isFull() {
            return (end + 1) % size == start;
        }
    }

    class MyCircularQueue2 {
        int[] data;
        int start;
        int end;
        int size;

        public MyCircularQueue2(int k) {
            size = k;
            data = new int[size];
            start = -1;
            end = -1;
        }

        public boolean enQueue(int value) {
            if (isFull()) {
                return false;
            }
            if (start == -1) {
                start = 0;
                end = 0;
                data[end] = value;
                return true;
            }
            end = (end + 1) % size;
            data[end] = value;
            return true;
        }

        public boolean deQueue() {
            if (isEmpty()) {
                return false;
            }
            if (start == end) {
                start = -1;
                end = -1;
                return true;
            }
            start = (start + 1) % size;
            return true;
        }

        public int Front() {
            if (isEmpty()) {
                return -1;
            }
            return data[start];
        }

        public int Rear() {
            if (isEmpty()) {
                return -1;
            }
            return data[end];
        }

        public boolean isEmpty() {
            return start == -1;
        }

        public boolean isFull() {
            return (end + 1) % size == start;
        }
    }
}
